package dt.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class HttpClientUtils {

    public static void main(String[] args) throws Exception {

    	
/*    	HttpClientUtils run = new HttpClientUtils();
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("username", "test1r");
		parameterMap.put("email", "hnqing@gmail.com");
		parameterMap.put("password", "hnHN123232");
		//String responseStr = run.post("https://manager.linode.com/session/signup_save", parameterMap);
		String responseStr = run.post("https://www.hn.10086.cn/Shopping/front/spike/querySubject.action?cache="+System.currentTimeMillis(), parameterMap);
		
		System.out.println(responseStr);*/
    	
    	HttpClientUtils run = new HttpClientUtils();
		Map<String, Object> parameterMap = new HashMap<String, Object>();

		dt.utils.ImageChange64Bit bit = new dt.utils.ImageChange64Bit();
		String image = bit.GetImageStr("d:\\test\\captcha.jpg");
		
		
/*		parameterMap.put("UserStr", "hnqbl|F2CF2740CEC181A0");
		parameterMap.put("GameID", "2004");
		parameterMap.put("TimeOut", "600");
		parameterMap.put("Rebate", "hnqbl|F2CF2740CEC181A0");
		parameterMap.put("DaiLi", "haoi");
		parameterMap.put("Kou", "0");
		parameterMap.put("BeiZhu", "");
		parameterMap.put("Ver", "web2");
		parameterMap.put("Key", "1234567890");
		parameterMap.put("Img", image);*/
		
		parameterMap.put("ID", "2004_11_05225217EC31381234");
		parameterMap.put("r", "2004_11_05225217EC31381234");
		
		
		//String responseStr = run.post("http://localhost:8080/property/service/call", parameterMap);
		//String responseStr = run.post("http://sv11.haoi23.net:8009/UPLOADBASE64.ASPX", parameterMap);
		String responseStr = run.post("http://sv11.haoi23.net:8009/GetAnswer.aspx", parameterMap);
		
		//http://2.haoi23.net/svlist.html

		System.out.println("报文返回："+responseStr);
    }
    
	/**
	 * POST请求
	 * 
	 * @param url
	 *            URL
	 * @param parameterMap
	 *            请求参数
	 * @return 返回结果
	 */
	public String post(String url, Map<String, Object> parameterMap) {
		String result = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			if (parameterMap != null) {
				for (Entry<String, Object> entry : parameterMap.entrySet()) {
					String name = entry.getKey();
					String value = (String) entry.getValue();
					if (StringUtils.isNotEmpty(name)) {
						nameValuePairs.add(new BasicNameValuePair(name, value));
					}
				}
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
			try {
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);
				EntityUtils.consume(httpEntity);
//				System.out.println(httpResponse.getStatusLine());
			} finally {
				httpResponse.close();
			}
		}catch (HttpHostConnectException e2){
			return post(url, parameterMap);
		}catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * GET请求
	 * 
	 * @param url
	 *            URL
	 * @param parameterMap
	 *            请求参数
	 * @return 返回结果
	 */
	public String get(String url, Map<String, Object> parameterMap) {
		String result = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			if (parameterMap != null) {
				for (Entry<String, Object> entry : parameterMap.entrySet()) {
					String name = entry.getKey();
					String value = (String) entry.getValue();
					if (StringUtils.isNotEmpty(name)) {
						nameValuePairs.add(new BasicNameValuePair(name, value));
					}
				}
			}
			HttpGet httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?") + EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));
			CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
			try {
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);
				EntityUtils.consume(httpEntity);
			} finally{
				httpResponse.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
